/*
 * Class NPCOption2
 *
 * Version 1.0
 *
 * Saturday, August 23, 2008
 *
 * Created by Palidino76
 */

package palidino76.rs2.io.packets;

import palidino76.rs2.Engine;
import palidino76.rs2.players.Player;
import palidino76.rs2.util.Misc;

public class NPCOption2 implements Packet {
    /**
     * Handles the second NPC option.
     * @param p The Player which the frame should be handled for.
     * @param packetId The packet id this belongs to.
     * @param packetSize The amount of bytes being recieved for this packet.
     */
    public void handlePacket(Player p, int packetId, int packetSize) {
        if (p == null || p.stream == null) {
            return;
        }
        if (!p.npcOption2) {
            int npcId = p.stream.readUnsignedWordBigEndianA();
            if (npcId <= 0 || npcId >= Engine.npcs.length || Engine.npcs[npcId] == null) {
                return;
            }
            p.clickId = npcId;
            p.clickX = Engine.npcs[npcId].absX;
            p.clickY = Engine.npcs[npcId].absY;
            if (Misc.getDistance(p.absX, p.absY, p.clickX, p.clickY) > 30) {
                return;
            }
            p.npcOption2 = true;
        }
        if (p.clickId <= 0 || p.clickId >= Engine.npcs.length || Engine.npcs[p.clickId] == null) {
            p.npcOption2 = false;
            return;
        }
        if (Misc.getDistance(p.absX, p.absY, p.clickX, p.clickY) > 1) {
            return;
        }
        p.npcOption2 = false;
        switch (p.clickId) {
        default:
            Misc.println("[" + p.username + "] Unhandled npc option 1: " + p.clickId);
            break;
        }
    }
}
